<?php
require(__META_CONTROLS_GEN__ . '/ComSchoolMetaControlGen.class.php');

/**

 * @package My QCubed Application
 * @subpackage MetaControls
 */
class ComSchoolMetaControl extends ComSchoolMetaControlGen {
    // Initialize fields with default values from database definition
    /*
		public function __construct($objParentObject, ComSchool $objComSchool) {
			parent::__construct($objParentObject,$objComSchool);
			if ( !$this->blnEditMode ){
				$this->objComSchool->Initialize();
			}
		}
    */
    public function calDate_Create($strControlId = null) {
        $this->calDate = new QDateTimeC($this->objParentObject, $strControlId);
        $this->calDate->Name = QApplication::Translate('Date'); 
        $this->calDate->__set("DateTime", $this->objComSchool->Date );
        return $this->calDate;
    }
    public function lstPeopleAsCom_Create($strControlId = null) {
        $this->lstPeopleAsCom = new AutoCompleteList($this->objParentObject, $strControlId);
        $this->lstPeopleAsCom->Name = QApplication::Translate('People');
        $objAssociatedArray = $this->objComSchool->GetPersonAsComArray();
        $objPersonArray = Person::LoadArrayByOrgId($_SESSION['OrgId']);
        if ($objPersonArray) foreach ($objPersonArray as $objPerson) {
                foreach ($objAssociatedArray as $objAssociated) {
                    if ($objAssociated->Id == $objPerson->Id)
                        $this->lstPeopleAsCom->lst->AddItem($objPerson->FirstName. " "  .$objPerson->LastName  ,$objPerson->Id);
                }

            }
        $this->lstPeopleAsCom->strCallList = "PersonOrg_Change";
        return $this->lstPeopleAsCom;
    }



    public function lstTypeObject_Create($strControlId = null) {
        parent::lstTypeObject_Create($strControlId);
        $this->lstTypeObject->Name = QApplication::Translate('Type of Communication');

    }



    protected $dttTime;

    protected function lstPeopleAsCom_Update() {
        if ($this->lstPeopleAsCom) {
            $this->objComSchool->UnassociateAllPeopleAsCom();
            $imax = $this->lstPeopleAsCom->lst->__get("ItemCount");
            for ($i=0; $i <$imax; $i++) {
                $objListItem = $this->lstPeopleAsCom->lst->GetItem($i);

                $this->objComSchool->AssociatePersonAsCom(Person::Load($objListItem->Value));
            }
        }
    }

    public function SaveComSchool() {
        try {

            //Prepare data for Record Info
            $this->dttTime = new QDateTime();
            $this->dttTime->setDate (date("Y"), date("m"), date("d"));
            $this->dttTime->setTime (date("H"), date("i"), date("s"));
            //Get Current user
            $CurrentUser = $_SESSION['User']['Id'];

            $EUser = $_SESSION['User']['Id'];
            // Update any fields for controls that have been created
            if ($this->calDate) $this->objComSchool->Date = $this->calDate->DateTime;
            if ($this->lstTypeObject) $this->objComSchool->Type = $this->lstTypeObject->SelectedValue;
            if ($this->txtEvent) $this->objComSchool->Event = $this->txtEvent->Text;
            if ($this->lstSample) $this->objComSchool->SampleId = $this->lstSample->SelectedValue;


            if ($this->blnEditMode) {
                //*** Update
                $this->objComSchool->UpdateBy   = $CurrentUser;
                $this->objComSchool->UpdateDate = $this->dttTime;
            }
            else {
                //**** New
                $this->objComSchool->Euser =  $EUser;
                $this->objComSchool->SchoolId = $_SESSION['OrgId'];
                $this->objComSchool->CreateBy   = $CurrentUser;
                $this->objComSchool->CreateDate = $this->dttTime;
            }
            $this->objComSchool->Save();
            // Finally, update any ManyToManyReferences (if any)
            $this->lstPeopleAsCom_Update();
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }
    }

}
?>